#include "player.h"

#include <cmath>
#include <cstdlib>
#include <algorithm> // min max

using std::min;
using std::max;

Player::Player(){}

Player::Player(GraphicsManager *gm) : GameObject(gm)
{
	speed = 0.0;
	_thrustx = _thrusty = 0.0f;
}

void Player::draw()
{
	float radi = 3;
	int segs = 3;

	for(int j=0; j<segs; j++)
	{
		float ang = headingAngle + (float)j * 2*3.14159f/(segs);
		float x1 = _x + cos(ang)*radi;
		float y1 = _y + sin(ang)*radi;


		ang = headingAngle + (float)(j+1) * 2*3.14159f/(segs);
		float x2 = _x + cos(ang)*radi;
		float y2 = _y + sin(ang)*radi;

		graphics->DrawLine(x1,y1,x2,y2,5.0f);
	}
}



void Player::lookAt( float playFieldMouseX, float playFieldMouseY )
{
	float dx = playFieldMouseX - _x;
	float dy = playFieldMouseY - _y;
	headingAngle = atan2f(dy,dx);
}

void Player::update( double dt )
{
	speed = 50;
	_x += _thrustx * speed * dt;
	_y += _thrusty * speed * dt;
	_thrustx *= 0.6;
	_thrusty *= 0.6;
}

void Player::setThrusters( float thrust_x, float thrust_y )
{
	this->_thrustx += thrust_x;
	this->_thrusty += thrust_y;
	thrust_x = min(1.0f, max(-1.0f, thrust_x));
	thrust_y = min(1.0f, max(-1.0f, thrust_y));
}

double Player::getx()
{
	return _x;
}

double Player::gety()
{
	return _y;
}

//bool Player::collidesWith( ICollideable other )
//{
//}
